using EnvDTE;
using System;
using System.Runtime.InteropServices;
using EnvDTE80;
using DslFactory.Utilities.DteCodeModel;
using System.Reflection;
using System.IO;

namespace DslFactory.Utilities.Test
{
 class Program
 {
  static void Main(string[] args)
  {
   // Get a reference on Visual Studio 2005
//   DTE dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.8.0");
   DTE dte = (DTE)Microsoft.VisualBasic.Interaction.CreateObject("VisualStudio.DTE.8.0", ""); // Cannot act on VS instance that debugs this program
   dte.MainWindow.Visible = true;
   if (dte == null)
   {
    Console.WriteLine("No Visual Studio 2005 is running");
   }
   else
   {
    Test(dte);
  //  dte.MainWindow.Close(vsSaveChanges.vsSaveChangesPrompt);
    Marshal.ReleaseComObject(dte);
   }
  }


  private static void Test(DTE dte)
  {
   Solution solution = dte.Solution;
   if (solution != null)
   {
    string solutionDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    string soluionFilename = solutionDirectory+Path.DirectorySeparatorChar+"TestSolution";
    if (!solution.IsOpen)
     if (File.Exists(soluionFilename))
      solution.Open(soluionFilename);
     else
     {
      solution.Create(solutionDirectory, "TestSolution");
      solution.SaveAs(soluionFilename+".sln");
     }
    Project project = IncrementalGenerator.EnsureProject(dte.Solution, "MyTestProject");
    ProjectItem sourceFile = IncrementalGenerator.EnsureSourcefile(project, "MyFile.cs");
    CodeNamespace ns = IncrementalGenerator.EnsureNamespace(sourceFile, "MyNamespace");
    CodeClass2 codeClass = IncrementalGenerator.EnsureClass(ns, "MyClass") as CodeClass2;
    IncrementalGenerator.EnsureUsings(codeClass, "System");
    IncrementalGenerator.EnsureAttribute(codeClass, "Serializable");
   // solution.Close(true);
   }
  }
 }
}
